<?php
/**
 * Created by PhpStorm.
 * User: 张鹏
 * Date: 2017-03-08
 * Time: 上午 10:58
 * Versions：v1.02
 * description:
 * 2017-05-05 新增 user_order_list 根据user_id 获取客户订单
 * 2017-05-15 修改 user_order_list
 */
namespace app\hongyanche\controller;


class Client
{
    //add_recharge_once 管理员生成会员一次性充值
    public function add_recharge_once(){
        $tel = input('tel');
        $res = model('Member')->addRechargeOnce($tel);
        return json($res);
    }
    //add_recharge_accumulate 累计消费生成会员
    public function add_recharge_accumulate(){
        $tel = input('tel');
        $res = model('Member')->addRechargeAccumulate($tel);
        return json($res);
    }

    /******************订单管理开始****************************/
    //order_create_by_admin  管理员生成充值订单
    public function recharge_order_create(){
        $user_id = input('user_id');
        $total_fee = input('total_fee');
        $res = model('Account')->orderCreateByAdmin($user_id, $total_fee);
        return json($res);
    }
    //recharge_order_record  获取用户充值记录
    public function recharge_order_record(){
        $filter['page'] = input('page');
        $filter['maxPerPage'] = input('maxPerPage');
        $filter['keywords'] = input('keyword');
        $filter['order'] = input('order');
        $demand = input('demand');
        $unpaid = input('unpaid');
        $channel = input('channel');
        $res = model('Account')->getRrecordListForAdmin($filter,$demand,$unpaid,$channel);
        return json($res);
    }
    //recharge_refund_record   账户退款清单
    public function recharge_refund_record(){
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $filer['keywords'] = input('keywords');
        $filer['select'] = input('select');
        $unpaid = input('unpaid');
        $channel = input('channel');
        $res = model('account')->getRefundList('recharge',$filer,$channel,$unpaid);
        return json($res);
    }
    /******************订单管理结束****************************/

    /******************会员卡管理开始****************************/
    //unbind 注销会员卡
    public function card_unbind(){
        $card_id = input('card_id');
        $res = model('card')->unbind($card_id);
        return json($res);
    }
    //record_list  获取会员卡操作记录
    public function card_record(){
        $filter['page'] = input('page');
        $filter['maxPerPage'] = input('maxPerPage');
        $filter['keywords'] = input('keywords');
        $filter['order'] = input('order');
        $res = model('card')->recordList($filter);
        return json($res);
    }
    //get_list 获取会员卡列表
    public function get_list(){
        $filter['page'] = input('page');
        $filter['maxPerPage'] = input('maxPerPage');
        $user_id = input('user_id');
        $res = model('card')->getCardList($filter,$user_id);
        return json($res);
    }
    //bind 绑定会员卡
    public function card_bind(){
        $user_id = input('user_id');
        $card_code = input('card_code');
        $password = input('password');
        $res = model('card')->bind($user_id,$card_code,$password);
        return json($res);
    }
    /******************会员卡管理结束****************************/

    /******************会员管理开始****************************/
    //用户获取会员的记录
    public function member_record(){
        $filter['page'] = input('page');
        $filter['maxPerPage'] =input('maxPerPage');
        $filter['keywords'] = input('keywords');
        $filter['order'] = input('order');
        $res = model('client')->memberList($filter);
        return json($res);
    }
    public function member_check(){
        $user_id = input('user_id');
        $res = model('Member')->create_check($user_id);
        return json($res);
    }
    public function member_filter(){
        $member = getConfig('member')[0];
        foreach ($member as $key=>$value){
            $member_filter[$value['group']]['group'] = $value['group'];
            $member_filter[$value['group']]['demand'][] = $value;
        }
        $res = ['state'=>1,'data'=>['rank'=>array_values($member_filter)]];
        return json($res);
    }
    /******************会员管理结束****************************/

    /*******************用户管理开始***************************/
    public function user_list(){
        $filter['page'] = input('page');
        $filter['maxPerPage'] =input('maxPerPage');
        $filter['keywords'] = input('keywords');
        $res = model('client')->userList($filter);
        return json($res);
    }
    //添加空账号
    public function user_add(){
        $res = model('user')->createByClientAdmin();
        return json($res);
    }
    //设置备注
    public function user_set_name(){
        $user_id = input('user_id');
        $newName = input('newname');
        $res = model('user')->setName($user_id,$newName);
        return json($res);
    }
    public function user_tel_bind(){
        $client_user_id = input('user_id');
        $tel = input('tel');
        $code = input('code');
        if(!model('Admin')->is('client')['state']){
            return json(['state' => 0, 'errormsg' => 'authority incorrect']);
        }
        if (!$client_user_id){
            return json(['state' => 0,'errormsg' => 'need user_id']);
        }
        if(!$tel){
            return json(['state' => 0,'errormsg' => 'need tel']);
        }
        $existRes = model('user')->exist(["tel"=>$tel]);
        if(isset($existRes['state']) && $existRes['state']){
            return json(['state' => 0,'errormsg' => 'tel exist']);
        }
        if ($code){
            $res = model('user')->bindingTelByClientAdmin($client_user_id,$tel,$code);
            return json($res);
        }
        $code = session("sms_binding_token");
        if(!$code){
            $result = model('Token')->add($demand = 'binding_tel', $data = array('content'=>$tel));
            if(!isset($result['state']) || !$result['state']){
                return json($result);
            }
            $code = $result['data']['code'];
            session("sms_binding_token",$code);
        }
        $sendsms = model("Sms")->send(
            $demand = "login",
            $data = array(
                'tel'=>$tel,
                'code'=>$code,
            )
        );
        if(!isset($sendsms['state']) || !$sendsms['state']){
            return json($sendsms);
        }
        session("sms_binding_token",null);
        return json($sendsms);
    }
    /*******************用户管理结束***************************/
    public function statistics(){
        $data['start_time'] = input('start_time');
        $data['end_time'] = input('end_time');
        $res = model('member')->statisticsMemberNum($data);
        return json($res);
    }
    //briefing  今日简报
    public function briefing(){
        $res = model('Member')->statisticsToday();
        return json($res);
    }
    //password_set  设置密码
    public function password_set(){
        $password = input('password');
        $card_code = input('card_code');
        $res = model('card')->passwordSet($password,$card_code);
        return json($res);
    }
    //客户交易记录查询
    public function user_order_list(){
        $filter['user_id'] = input('user_id');
//        $filter['start'] = input('start');
//        $filter['end'] = input('end');
//        $filter['order'] = input('order');
        $filter['page'] = input('page');
        $res = model('client')->consume($filter);
        return json($res);
    }
    public function uesr_recharge(){
        $filter['user_id'] = input('user_id');
        $filter['start'] = input('start');
        $filter['end'] = input('end');
        $filter['page'] = input('page');
        $filter['maxPerPage'] =input('maxPerPage');
        $res = model('client')->uesrRecharge($filter);
        return json($res);
    }
    public function member_info(){
        $filter['user_id'] = model('user')->getUserId();
        $filter['page'] = input('page');
        $res = model('client')->consume($filter);
        return json($res);
    }
}